import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {CommonTool} from '../../common/common.tool';
import {HTTP_URLS} from '../../base/base-http.url';

@Injectable({
  providedIn: 'root'
})
export class FileManagementService {

  constructor(private http: HttpClient) {

  }

  /**
   * 创建目录
   */
  public createFolder(req: any): Observable<Array<any>> {
    return this.http.post<Array<any>>(HTTP_URLS.createCurrentUserFolder, req);
  }

  /**
   * 查询目录集合
   */
  public getUserFolderFile(req): Observable<Array<any>> {
    const url = CommonTool.analysisParam(HTTP_URLS.getCurrentUserFolderFile, req);
    return this.http.get<Array<any>>(url);
  }

  /**
   * 删除文件夹
   */
  public deleteFolder(folderId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.deleteCurrentUserFolder, {folderId});
    return this.http.delete<any>(url);
  }

  /**
   * 删除文件夹
   */
  public deleteFile(fileId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.deleteCurrentUserFile, {fileId});
    return this.http.delete<any>(url);
  }

  /**
   * 查询目录集合信息
   */
  public getUserFolderList(req) {
    const url = CommonTool.analysisParam(HTTP_URLS.getCurrentUserFolders, req);
    return this.http.get<any>(url);
  }

  /**
   * 移动目录
   */
  public updateUserFolderFile(folderId, req): Observable<any> {
    const url = CommonTool.analysisParam(HTTP_URLS.updateCurrentUserFolderFile, {folderId});
    return this.http.put<any>(url, req);
  }

  /**
   * 复制文件
   */
  public createUserFolderFile(data, req) {
    const url = CommonTool.analysisParam(HTTP_URLS.createCurrentUserFolderFile, data);
    return this.http.post<any>(url, req);
  }

  /**
   * 修改文件
   */
  public updateUserFile(id, req): Observable<any> {
    const url = CommonTool.analysisUrl(HTTP_URLS.updateCurrentUserFile, {fileId: id});
    return this.http.put<any>(url, req);
  }

  /**
   * 修改目录
   */
  public updateUserFolder(id, req): Observable<any> {
    const url = CommonTool.analysisUrl(HTTP_URLS.updateCurrentUserFolder, {folderId: id});
    return this.http.put<any>(url, req);
  }

  /**
   * 创建用户的文件包
   * 打包文件
   */
  public createUserFilePackage(folderId, req) {
    const url = CommonTool.analysisUrl(HTTP_URLS.createCurrentUserFolderZipPackage, {folderId});
    return this.http.post<any>(url, req);
  }

  /**
   * 分享文件
   */
  public createUserShare(req: { fileIds, folderIds, needCode; validTime, remark }): Observable<any> {
    return this.http.post<any>(HTTP_URLS.createCurrentUserShare, req);
  }

  /**
   * 查询共享文件
   *
   */
  public getUserSharePage(current, req): Observable<any> {
    let url = CommonTool.analysisUrl(HTTP_URLS.getCurrentUserSharePage, {current});
    url = CommonTool.analysisParam(url, req);
    return this.http.get(url);
  }

  /**
   * 删除共享文件
   */
  public deleteUserShare(shareId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.deleteCurrentUserShare, {shareId});
    return this.http.delete<any>(url);
  }

  /**
   * 分页查询链接
   */
  public getUserSafetyChainPage(current, req) {
    let url = CommonTool.analysisUrl(HTTP_URLS.getCurrentUserSafetyChainPage, {current});
    url = CommonTool.analysisParam(url, req);
    return this.http.get<any>(url);
  }

  /**
   * 创建安全链接
   */
  public createUserSafetyChain(req) {
    return this.http.post<any>(HTTP_URLS.createCurrentUserSafetyChain, req);
  }

  /**
   * 更新安全链接
   */
  public updateUserSafetyChain(id, req) {
    const url = CommonTool.analysisUrl(HTTP_URLS.updateCurrentUserSafetyChain, {safetyChainId: id});
    return this.http.put<any>(url, req);
  }

  /**
   * 删除安全链接
   */
  public deleteUserSafetyChain(id) {
    const url = CommonTool.analysisUrl(HTTP_URLS.deleteCurrentUserSafetyChain, {safetyChainId: id});
    return this.http.delete<any>(url);
  }

  /**
   * 查询共享文件
   */
  public getShareFolderFile(shareId, req) {
    let url = CommonTool.analysisUrl(HTTP_URLS.getShareFolderFile, {shareId});
    url = CommonTool.analysisParam(url, req);
    return this.http.get<any>(url);
  }

  /**
   * 获取系统文件集合
   */
  public getShareFileByCode(code, drawCode?) {
    let url = CommonTool.analysisUrl(HTTP_URLS.getShareByCode, {code});
    url = CommonTool.analysisParam(url, {drawCode});
    return this.http.get<any>(url);
  }

  /**
   * 获取系统文件集合
   */
  public getUploadUrl() {
    return HTTP_URLS.createCurrentUserFile;
  }

  /**
   * 创建app文件
   */
  public getAppUploadUrl(appId) {
    return CommonTool.analysisUrl(HTTP_URLS.createAppFile, {appId});
  }

  /**
   * 获取文件夹大小
   */
  public getUserFolderSize(folderId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.getCurrentUserFolderSize, {folderId});
    return this.http.get<any>(url);
  }

  /**
   * 获取用户所有父级文件夹
   */
  public getUserParentFolder(folderId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.getCurrentUserParentFolder, {folderId});
    return this.http.get<any>(url);
  }

  /**
   * 获取共享的所有父级目录
   */
  public getShareParentFolder(shareId, folderId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.getShareParentFolder, {shareId, folderId});
    return this.http.get<any>(url);
  }

  /**
   * 获取共享文件夹大小
   */
  public getShareFolderSize(shareId, folderId) {
    const url = CommonTool.analysisUrl(HTTP_URLS.getShareFolderSize, {shareId, folderId});
    return this.http.get<any>(url);
  }

  /**
   * 创建当前用户的cdn地址
   */
  public createCdnUrl(req) {
    return this.http.post<any>(HTTP_URLS.createCdnUrl, req);
  }

  /**
   * 获取当前用户网盘信息
   */
  public getUserDiskInfo() {
    return this.http.get<any>(HTTP_URLS.getUserDiskInfo);
  }
}
